package edu.sctu.xuping.month06.aboutinternet;


import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

/**
 * Created by user02
 */
public class HttpConnectionUtil {
    /**
     * 获取指定网址上的html代码
     *
     * @param path
     * @return
     * @throws IOException
     */
    public String getHttpUtil(String path) throws IOException {
        int count = 0;
        URL url = new URL(path);
        byte[] buffer = new byte[1024];
        InputStream inputstream = url.openStream();
        while ((count = inputstream.read(buffer)) != -1) {
            System.out.println(new String(buffer, 0, count, "utf-8"));
        }
        inputstream.close();
        return path;
    }

    public void getUrlConnection(String path) throws IOException {
        int count = 0;
        byte[] buffer = new byte[1024];
        URL url = new URL(path);
        URLConnection urlconnection = url.openConnection();
        urlconnection.setRequestProperty("Content-Type", "xxxxx");
        urlconnection.setRequestProperty("Accept-Charset", "utf-8");
        urlconnection.connect();
        System.out.println(urlconnection.getContentType() + ":" + urlconnection.getContentEncoding());
        InputStream inputstream = urlconnection.getInputStream();
        while ((count = inputstream.read(buffer)) != -1) {
            System.out.println(new String(buffer, 0, count, "utf-8"));
        }
        inputstream.close();
    }

    public void getUrlConnectionPost(String path) throws IOException {
        int count = 0;
        byte[] buffer = new byte[1024];
        URL url = new URL(path);
        URLConnection urlconnection = url.openConnection();
        urlconnection.setDoInput(true);
        urlconnection.connect();
        OutputStream outputstream = urlconnection.getOutputStream();
        //outputstream.write(param.getBytes());
        outputstream.flush();
        System.out.println(urlconnection.getContentType() + ":" + urlconnection.getContentEncoding());
        InputStream inputstream = urlconnection.getInputStream();
        while ((count = inputstream.read()) != -1) {
            System.out.println(new String(buffer, 0, count, "utf-8"));
        }
    }

    public void getHttpUrlConnection(String path) throws IOException {
        int count = 0;
        byte[] buffer = new byte[1024];
        URL url = new URL(path);
        HttpURLConnection httpurlconnection = (HttpURLConnection) url.openConnection();
        InputStream inputstream = httpurlconnection.getInputStream();
        httpurlconnection.connect();
        while ((count = inputstream.read(buffer)) != -1) {
            System.out.println(new String(buffer, 0, count, "utf-8"));
        }
        inputstream.close();
    }

    public void getHttpUrlConnectionPost(String path) throws IOException {
        int count = 0;
        byte[] buffer = new byte[1024];
        URL url = new URL(path);
        HttpURLConnection httpurlconnection = (HttpURLConnection) url.openConnection();
        httpurlconnection.setDoInput(true);
        httpurlconnection.setDoOutput(true);
        httpurlconnection.connect();
        OutputStream outputstream = httpurlconnection.getOutputStream();
        outputstream.flush();
        outputstream.write(Integer.parseInt(path));
        InputStream inputstream = httpurlconnection.getInputStream();
        while ((count = inputstream.read(buffer)) != -1) {
            System.out.println(new String(buffer, 0, count, "utf-8"));
        }
        inputstream.close();
    }
}
